// pages/home/home.js
import {MD5} from '../../utils/MD5';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageList: [
        { src: '/icon/huiyi2.jpg' },
        { src: '/icon/test2.jpg' },
        { src: '/icon/test3.jpg' },
    ],
    fileList:[]
},
generateToken: function(appId, appKey, timestamp){
  const param = "ApiRequest{appId="+appId+",appKey="+appKey+",timeStamp="+timestamp+"}";
  const tokenStr=MD5.instance.hex_md5(param);
  return tokenStr;
},
setList:function(fileName){
this.setData({
  fileList:fileList+fileName
})
},
fileList: function(){
  const that = this;
      wx.request({
        url: 'http://localhost:8080/files/files/list',
        method: 'GET',
        header: {
          'content-type': 'application/json', // 设置内容类型
          'Authorization': `Bearer ${wx.getStorageSync('ruoyiToken')}` // 在请求头中加入token，注意Bearer和token之间有一个空格
        },
        success:function(res){
          console.log(res.data)
          const filesArry = res.data.rows
          const filteredFiles = filesArry.filter(file => {
            // 过滤掉后缀为.jpg的文件
            return !file.fileName.endsWith('.jpg');
          });
          const newFileList = filteredFiles.map(file => {
            return {
              fileName: file.fileName, // 假设文件名的属性是 fileName
              filePath: file.filePath, // 假设文件路径的属性是 filePath
              fileId: file.fileId
            };
          });
          that.setData({
            fileList: that.data.fileList.concat(newFileList) // 使用 concat 方法合并数组
          })
          console.log(that.data.fileList)
      }
    })
},
onFileTap: function(e) {
  const fileId = e.currentTarget.dataset.fileId; // 获取点击的文件ID
    const filePath = e.currentTarget.dataset.filePath; // 获取点击的文件路径
    const fileName = e.currentTarget.dataset.fileName;
console.log(fileName)
    // 执行访问服务器的操作，例如预览文件
    this.previewFile(fileId,fileName);
},
previewFile: function(fileId,fileName) {
    wx.downloadFile({
      url: 'http://localhost:8080/files/files/downloadById?fileId='+fileId+'&delete=false',
      filePath: wx.env.USER_DATA_PATH+'/'+fileName,
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + wx.getStorageSync('ruoyiToken') // 携带token的header
      },
      success: function(res) {
        if (res.statusCode === 200) {
          const tempFilePath = res.filePath;
          console.log(res)
          wx.openDocument({
            filePath: tempFilePath,
            showMenu: true
          });
          console.log(tempFilePath)
        }
      }
    });
},
downloadFile2: function(file) {
  wx.downloadFile({
    url: 'http://119.91.204.198:8081/file/api/download?fileId=' + file.fileId + '&appId=' + appId + '&timeStamp=' + timestamp + '&token=' + token,
    //filePath:wx.env.USER_DATA_PATH+'/test.jpg',
    success: function(res) {
      if (res.statusCode === 200) {
        console.log(res)
        const filePath = res.tempFilePath;
        console.log(filePath)
        wx.previewImage({
          current: filePath,
          urls: [filePath]
        })
      }
    }
  });
},
fileUpload: function(){
  // 上传文件
  wx.chooseMessageFile({
    count: 1,
    type: 'all',
    success: (chooseRes)=>{
      const tempFile = chooseRes.tempFiles[0]; // 选择的第一个文件
        console.log(tempFile);
      const realFilePath = tempFile.path
      console.log(realFilePath)
      wx.uploadFile({
        filePath: realFilePath,
        name: 'file',
        url: 'http://localhost:8080/files/files/add', 
        formData: {
          'fileName': tempFile.name // 原始文件名
        },
        header: {'content-type':'multipart/form-data'},
        success:(res) => {
          console.log(res); // 服务器返回的数据
        },
        fail:(res) =>{
          console.log("fal")
          console.log(res); // 服务器返回的数据
        },
        progressUpdate: function (res) {
          if (res.progress > 0) {
            console.log('上传进度: ' + res.progress + '%');
          }
        }
      });
    }
  });
},
fileDownload: function () {  // 点击选择文件按钮触发事件
  wx.downloadFile({
    url: 'http://localhost:8080/common/download?fileName=huiyi2.jpg&delete=false', 
    filePath:wx.env.USER_DATA_PATH+'/huiyi2.jpg',
    success: function (res) {
      console.log(res)
      const data111 = res.filePath
      console.log(data111)
      wx.previewImage({
        current: data111,
        urls: [data111]
      })
    }
  })
},
goToClass:function(){
  wx.navigateTo({
    url: '../vocationalcollege/college',
    success: (res) => {
      console.log('跳转成功');
    },
    fail: (err) => {
      console.error('跳转失败', err);
    }
  })
},
foreign:function name(params) {
  wx.navigateTo({
    url: '../foreignLanguages/foreignLanguages',
    success: (res) => {
      console.log('跳转成功');
    },
    fail: (err) => {
      console.error('跳转失败', err);
    }
  })
},
taskCity:function name(params) {
  wx.navigateTo({
    url: '../examLocations/examLocations',
    success: (res) => {
      console.log('跳转成功');
    },
    fail: (err) => {
      console.error('跳转失败', err);
    }
  })
},
candidateType:function(){
  wx.navigateTo({
    url: '/pages/studentType/studentType' ,// 跳转到首页
    success: (res) => {
      console.log('跳转成功');
    },
    fail: (err) => {
      console.error('跳转失败', err);
    }
  })
},
gotoGet:function(){
  wx.navigateTo({
    url: '../volunteer/volunteer',
    success: (res) => {
      console.log('跳转成功');
    },
    fail: (err) => {
      console.error('跳转失败', err);
    }
  })
},
handleImageTap: function(e) {
        // wx.navigateTo({
        //     url: '/pages/activity2/activity2', // 替换为你要跳转的新页面路径
        // });
        wx.navigateTo({
          url: 'https://www.nm.zsks.cn/zxyw/202411/t20241105_45361.html',
        });
},
getTo: function(){
  wx.request({
    url: 'https://www.nm.zsks.cn/zxyw/202411/t20241105_45361.html',
    method: 'GET'
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.fileList()
  },
  
})